package com.neuedu.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.neuedu.dao.ITypeDAO;
import com.neuedu.entity.Type;
import com.neuedu.sql.QueryUpdate;
import com.neuedu.sql.ResultSetObject;

public class TypeDAOImp implements ITypeDAO {

	@Override
	public List<Type> selectAll(Integer tflag,int offSet, int pageNum) {
	
		 String sql="select * from t_type where tflag=? limit ?,?";
		  List<Type> types=QueryUpdate.query(
				  new ResultSetObject<Type>() {

					@Override
					public Type getObject(ResultSet rs) throws SQLException {
						Type t=new Type();
						t.setTid(rs.getInt("tid"));
						t.setTname(rs.getString("tname"));
						t.setTflag(rs.getInt("tflag"));
						t.setCreatetime(rs.getTimestamp("createtime"));
						t.setUpdatetime(rs.getTimestamp("updatetime"));
						return t;
					}
				}, sql,tflag,offSet,pageNum);
			return types;
	}

	@Override
	public List<Type> selectAll(Integer tflag) {
	  String sql="select * from t_type where tflag=? ";
	  List<Type> types=QueryUpdate.query(
			  new ResultSetObject<Type>() {

				@Override
				public Type getObject(ResultSet rs) throws SQLException {
					Type t=new Type();
					t.setTid(rs.getInt("tid"));
					t.setTname(rs.getString("tname"));
					t.setTflag(rs.getInt("tflag"));
					t.setCreatetime(rs.getTimestamp("createtime"));
					t.setUpdatetime(rs.getTimestamp("updatetime"));
					return t;
				}
			}, sql,tflag);
		return types;
	}

	@Override
	public boolean insert(Type type) {
		String sql="insert into t_type(tname,tflag,createtime,updatetime)" + 
				"			values(?,?,?,?)";
		
		boolean flag=QueryUpdate.update(sql, type.getTname(),type.getTflag(),type.getCreatetime(),type.getUpdatetime());
		return flag;
	}

	@Override
	public boolean delete(Integer tid) {
		String sql="delete from t_type where tid=?";
		
		boolean flag=QueryUpdate.update(sql, tid);
		return flag;
	}

	@Override
	public boolean deleteUpdate(Integer tid) {
	  String sql="update t_type set tflag=1 where tid=?";
	  boolean flag=QueryUpdate.update(sql, tid);
		return flag;
	}

	@Override
	public Type selectOneById(Integer tid) {
	
		String sql="select * from t_type where tid=?";
		  List<Type> typelists=	QueryUpdate.query(new ResultSetObject<Type>() {

				@Override
				public Type getObject(ResultSet rs) throws SQLException {
					Type t=new Type();
					t.setTid(rs.getInt("tid"));
					t.setTname(rs.getString("tname"));
					t.setTflag(rs.getInt("tflag"));
					t.setCreatetime(rs.getTimestamp("createtime"));
					t.setUpdatetime(rs.getTimestamp("updatetime"));
					return t;
				}
			}, sql,tid);
		  
		  if(typelists==null) {
			 return null;
		  }
		  return typelists.get(0);
	}

	@Override
	public boolean update(Type type) {
		 String sql="update t_type set tname=? where tid=?";
		  boolean flag=QueryUpdate.update(sql, type.getTname(),type.getTid());
			return flag;
	}

	@Override
	public List<Type> selectAll(int offSet, int pageNum) {
		 String sql="select * from t_type limit ?,?";
		  List<Type> types=QueryUpdate.query(
				  new ResultSetObject<Type>() {

					@Override
					public Type getObject(ResultSet rs) throws SQLException {
						Type t=new Type();
						t.setTid(rs.getInt("tid"));
						t.setTname(rs.getString("tname"));
						t.setTflag(rs.getInt("tflag"));
						t.setCreatetime(rs.getTimestamp("createtime"));
						t.setUpdatetime(rs.getTimestamp("updatetime"));
						return t;
					}
				}, sql,offSet,pageNum);
			return types;
	}

	@Override
	public List<Type> selectAll() {
		
		 String sql="select * from t_type";
		  List<Type> types=QueryUpdate.query(
				  new ResultSetObject<Type>() {

					@Override
					public Type getObject(ResultSet rs) throws SQLException {
						Type t=new Type();
						t.setTid(rs.getInt("tid"));
						t.setTname(rs.getString("tname"));
						t.setTflag(rs.getInt("tflag"));
						t.setCreatetime(rs.getTimestamp("createtime"));
						t.setUpdatetime(rs.getTimestamp("updatetime"));
						return t;
					}
				}, sql);
			return types;
	}

	@Override
	public int selectCount(Integer tflag) {
		String sql="select count(*) from t_type where tflag=?";
		List<Integer> count=QueryUpdate.query(new ResultSetObject<Integer>() {

			@Override
			public Integer getObject(ResultSet rs) throws SQLException {
				return rs.getInt(1);
			}
		}, sql, tflag);
		if(count==null) {
			return 0;
		}
		return count.get(0);
	}

	@Override
	public int selectCount() {
		String sql="select count(*) from t_type ";
		List<Integer> count=QueryUpdate.query(new ResultSetObject<Integer>() {

			@Override
			public Integer getObject(ResultSet rs) throws SQLException {
				return rs.getInt(1);
			}
		}, sql);
		if(count==null) {
			return 0;
		}
		return count.get(0);
	}

}
